#include <mpi.h>
#include <stdio.h>
#include <unistd.h>


int main(int argc, char** argv){
    int rank, nproc;
    int TAG = 100;
    MPI_Request send_request, recv_request;
    MPI_Status send_status, recv_status;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int isend[32], irecv[32];
    printf("Before : My rank = %d  <",rank );
    for(int i=0; i<nproc; i++)
        printf(" %d ", isend[i] = i + nproc * rank);
    printf( " >\n");

    for (int i = 0; i < nproc; i++){
        if (rank == i){
            irecv[i] = isend[i];
        }
        else{
            MPI_Send(isend+i, 1, MPI_INT, i, TAG, MPI_COMM_WORLD);
        }
    }
    for (int i = 0; i < nproc; i++){
        if (rank != i)
            MPI_Recv(irecv+i, 1, MPI_INT, i, TAG, MPI_COMM_WORLD, &recv_status);
    }

    printf("After  : My rank = %d  <", rank);                        
    for(int i=0; i<nproc; i++)
        printf(" %d ", irecv[i]);
    printf(" >\n");
    MPI_Finalize();
    


    return 0;
}